from sanic import response

from mysql_.Orm_DB import *
from view.register import bp1
from my_jinja2.my_jinja2 import template


@bp1.route('/findback1', methods=['GET', 'POST'])  # 找回密码第一个界面：输入用户名，存在就返回用户的密保问题，错误返回用户名错误
async def findback1(request):
    global username_,data
    username_ = request.form.get('username')
    data = db_query(username_)
    if data == False:
        dict={'key':'userfail'}
        return response.json(dict)
    else:
        print('用户存在。')
        dict={'key':'success','message':data['secret_problem']}
        return response.json(dict)


@bp1.route('/findback2', methods=['GET', 'POST'])    # 找回密码第二个界面判断输入的密保问题，成功返回 success，错误返回 fail
async def findback2(request):
 #   secret_prob = request.form.get('secret_prob')
    secret_ans = request.form.get('answer')
    print(secret_ans)
    print(data['secret_answer'])
    if  secret_ans == data['secret_answer']:
        dict={'key':'success'}
        return response.json(dict)
    else:
        dict={'key': 'fail'}
        return response.json(dict)


@bp1.route('/findback3', methods=['GET', 'POST'])  # 找回密码第三个界面  获取新密码。
async def findback3(request):
    password = request.form.get('new_password1')
    modify=db_modify(username_,password)
    print(password)
    if modify == True:
        print(modify)
        return response.text('success')

    # if request.form.get('submit') == '提交':
    #     username = request.form.get('username')
    #     ques = request.form.get('ques')
    #     ans = request.form.get('ans')
    #     if not all([username, ques, ans]):
    #         print('输入不完整')
    #         redirect(findback)
    #     else:
    #         data = {''}
    #
    #     return redirect('login')
    # return template('findback.html')
